#include "versionInfo.h"
#include "configargs.h"
#include "hardinterfacetest/hardinterfacetest.h"
#include <QFile>
#include <stdlib.h>
#include <stdio.h>

QString readSubQss()
{
    QString iconQssStr;
    QString sizeQssStr;
    if(g_lcdsize == LCDSIZE_1280x800){
        QFile sizeQss(":/qss/linuxdesktop/stylesheet/subwidgetsize_1024x768.qss");
        QFile iconQss(":/qss/linuxdesktop/stylesheet/subwidgeticon_1024x768.qss");
        sizeQss.open(QFile::ReadOnly);
        sizeQssStr = sizeQss.readAll();

        iconQss.open(QFile::ReadOnly);
        iconQssStr = iconQss.readAll();
    }else if(g_lcdsize == LCDSIZE_1024x768){
        QFile sizeQss(":/qss/linuxdesktop/stylesheet/subwidgetsize_1024x768.qss");
        QFile iconQss(":/qss/linuxdesktop/stylesheet/subwidgeticon_1024x768.qss");
        sizeQss.open(QFile::ReadOnly);
        sizeQssStr = sizeQss.readAll();

        iconQss.open(QFile::ReadOnly);
        iconQssStr = iconQss.readAll();
    }else if(g_lcdsize == LCDSIZE_800x600){
        QFile sizeQss(":/qss/linuxdesktop/stylesheet/subwidgetsize_800x600.qss");
        sizeQss.open(QFile::ReadOnly);
        sizeQssStr = sizeQss.readAll();

        QFile iconQss(":/qss/linuxdesktop/stylesheet/subwidgeticon_800x600.qss");
        iconQss.open(QFile::ReadOnly);
        iconQssStr = iconQss.readAll();
    }else   if(g_lcdsize == LCDSIZE_800x480){
        QFile sizeQss(":/qss/linuxdesktop/stylesheet/subwidgetsize7b.qss");
        sizeQss.open(QFile::ReadOnly);
        sizeQssStr = sizeQss.readAll();
        QFile iconQss(":/qss/linuxdesktop/stylesheet/subwidgeticon7b.qss");
        iconQss.open(QFile::ReadOnly);
        iconQssStr = iconQss.readAll();
    }else   if(g_lcdsize == LCDSIZE_480x272){
        QFile sizeQss(":/qss/linuxdesktop/stylesheet/subwidgetsize_480x272.qss");
        sizeQss.open(QFile::ReadOnly);
        sizeQssStr = sizeQss.readAll();
        QFile iconQss(":/qss/linuxdesktop/stylesheet/subwidgeticon_480x272.qss");
        iconQss.open(QFile::ReadOnly);
        iconQssStr = iconQss.readAll();
    }

    return (sizeQssStr + iconQssStr);

}

versionClass::versionClass(QWidget *parent) : JDialog(parent)
{
    QGridLayout *mainHLayout = new QGridLayout(this);
    QGridLayout *pSubLayout = new QGridLayout;
    QVBoxLayout *VBoxLayout = new QVBoxLayout;


    pFrame = new QFrame(this);
    process = new QProcess(this);
    initLabel();

    VBoxLayout->addWidget(ubootVersionLabel);
    VBoxLayout->addWidget(kernelVersionLabel);
    VBoxLayout->addWidget(appVersionLabel);
    VBoxLayout->addWidget(libVersionLabel);

    initRetBtn();
    VBoxLayout->addWidget(returnMainWindowBut);


    pSubLayout->addLayout(VBoxLayout, 0,0,1,1);

    pFrame->setLayout(pSubLayout);

    mainHLayout->addWidget(pFrame, 0, 0, 1, 1);

    pFrame->setObjectName("versionFrame");
    this->setJLayout(mainHLayout);
    this->setJTitle(tr("version"));
    this->setStyleSheet(readSubQss());
}
versionClass::~versionClass()
{
    delete ubootVersionLabel;
    delete kernelVersionLabel;
    delete appVersionLabel;
    delete libVersionLabel;

    delete process;
    delete pFrame;
}

void versionClass::initLabel()
{
    ubootVersionLabel = new QLabel(this);
    kernelVersionLabel = new QLabel(this);
    appVersionLabel = new QLabel(this);
    libVersionLabel = new QLabel(this);
    //this->setAttribute(Qt::WA_DeleteOnClose);

    ubootVersionLabel->setText(tr("boot版本：")+getUbootVersion());
    kernelVersionLabel->setText(tr("kernel版本：")+getKernelVersion());
    appVersionLabel->setText(tr("APP版本：")+DEF_APP_VERSION_STR);
    QFile file("/etc/version.txt");
    if(!file.open(QFile::ReadOnly)){
        QMessageBox::critical(NULL, "Error", "Version file open error", QMessageBox::Yes);
    }
    QString versionString = QString::fromLatin1(file.readLine());//qt char转QString
    qDebug("versionString2: %s", qPrintable(versionString));


    QString authString = QString::fromLatin1(file.readLine());//qt char转QString
    qDebug("file authString: %s", qPrintable(authString));//add for test

    if(0 == authVersionString(versionString, authString)){
        libVersionLabel->setText(tr("SYS版本：")+versionString);
    }else{
        QMessageBox::critical(NULL, "Error", "sys version error", QMessageBox::Yes);
    }

    ubootVersionLabel->setAlignment(Qt::AlignCenter);
    kernelVersionLabel->setAlignment(Qt::AlignCenter);
    appVersionLabel->setAlignment(Qt::AlignCenter);
    libVersionLabel->setAlignment(Qt::AlignCenter);
}

void versionClass::initRetBtn()
{
    returnMainWindowBut = new QPushButton(this);
    returnMainWindowBut->setText(tr("  退出  "));
    connect(returnMainWindowBut, SIGNAL(released()), this, SLOT(close()));
}

QString versionClass::getKernelVersion()
{
    process->start("/bin/sh /etc/get_kernel_version.sh");
    process->waitForStarted();
    process->waitForFinished(5000);
    QString procResult = process->readLine();
    return procResult;
}

QString versionClass::getUbootVersion()
{
    process->start("/bin/sh /etc/get_uboot_version.sh");
    process->waitForStarted();
    process->waitForFinished(5000);
    QString procResult = process->readLine();
    return procResult;
}

int versionClass::authVersionString(QString versionString, QString authString)
{
    int i;
    int sum=0;
    unsigned long long x_sum=1;
    char tmpBuf[128];

    qDebug("in auto string: %s", qPrintable(authString));
    if(versionString.contains("_")){
        QString tmpVersionString = versionString.right(18);
        for(i=0; i < tmpVersionString.length()-1; i++){
            qDebug("str %c [%d]:%u", tmpVersionString.at(i), i, (unsigned int)(tmpVersionString.at(i).toAscii()));//add for test
            sum += (int)(tmpVersionString.at(i).toAscii());
            x_sum = x_sum * (unsigned long long)(tmpVersionString.at(i).toAscii());
        }
        qDebug("sum is: %d, x_sum: %lld", sum, x_sum);//add for test
        snprintf(tmpBuf, sizeof(tmpBuf), "%lld", x_sum);
        qDebug("%s", tmpBuf);

        QString tmpString=QString(tmpBuf);

        if(tmpString.contains("-")){
            tmpString = tmpString.right(tmpString.length()-1);
            qDebug("tmpString:%s", qPrintable(tmpString));//add for test
        }

        QString myAuthString = QString().sprintf("%d", sum);
        myAuthString.append(tmpString);
        qDebug("myAuthString: %s", qPrintable(myAuthString));//add for test
        if(myAuthString.contains(authString)||authString.contains(myAuthString)){
            qDebug("auth OK");
            return 0;
        }else{
            qDebug("auth Failed");
            return -1;
        }
    }else{
        return -1;
    }
}

//void versionClass::versionClass()
//{
//	qDebug("tsTest\n");

//	process->start("ts_test");

//	process->waitForStarted();
//	process->waitForFinished(-1);

//    this->update();
//}
void versionClass::slot_return()
{
    close();
}
